package com.etoak.car.controller;

import com.etoak.car.bean.Gift;
import com.etoak.car.bean.vo.GiftCountResp;
import com.etoak.car.bean.vo.GiftReq;
import com.etoak.car.bean.vo.GiftResp;
import com.etoak.car.service.GiftService;
import com.etoak.commons.enums.ETEnums;
import com.etoak.commons.page.Page;
import com.etoak.commons.response.ETResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/gift")
public class GiftController {

    @Autowired
    private GiftService giftService;

    @PostMapping("/addGift")
    public ETResponse addGift(@RequestBody Gift gift){
        int flag = giftService.addGift(gift);

        ETResponse response = new ETResponse();
        if (flag<0){
            response.setCode(ETEnums.ERROR.getCode());
            response.setMsg(ETEnums.ERROR.getMsg());
            response.setData("添加失败");
        }
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData("添加成功");
        return response;
    }

    @GetMapping("/querySome")
    public ETResponse querySome(@RequestParam(required = false,defaultValue = "1") int pageNum, @RequestParam(required = false,defaultValue = "2") int pageSize){
        Page<Gift> gifts = giftService.querySome(pageNum,pageSize);

        ETResponse response = new ETResponse();
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData(gifts);
        return response;
    }

    @GetMapping("/del/{id}")
    public ETResponse delGift(@PathVariable int id){
        int flag = giftService.delGift(id);

        ETResponse response = new ETResponse();
        if (flag<0){
            response.setCode(ETEnums.ERROR.getCode());
            response.setMsg(ETEnums.ERROR.getMsg());
            response.setData("删除失败");
        }
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData("删除成功");
        return response;
    }

    @PostMapping("/updateGift")
    public ETResponse updateGift(@RequestBody Gift gift){
        int flag = giftService.updateGift(gift);

        ETResponse response = new ETResponse();
        if (flag<0){
            response.setCode(ETEnums.ERROR.getCode());
            response.setMsg(ETEnums.ERROR.getMsg());
            response.setData("修改失败");
        }
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData("修改成功");
        return response;
    }

    @GetMapping("/getDataById/{id}")
    public ETResponse getDataById(@PathVariable int id){
        Gift gift = giftService.getDataById(id);
        ETResponse response = new ETResponse();
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData(gift);
        return response;
    }

    @GetMapping("/querySomeByUser")
    public ETResponse querySomeByUser(@RequestParam(required = false,defaultValue = "1") int pageNum, @RequestParam(required = false,defaultValue = "2") int pageSize){
        Page<GiftResp> gifts = giftService.querySomeByUser(pageNum,pageSize);

        ETResponse response = new ETResponse();
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData(gifts);
        return response;
    }


    @GetMapping("/delUserGift/{id}")
    public ETResponse delUserGift(@PathVariable int id){
        int flag = giftService.delUserGift(id);

        ETResponse response = new ETResponse();
        if (flag<0){
            response.setCode(ETEnums.ERROR.getCode());
            response.setMsg(ETEnums.ERROR.getMsg());
            response.setData("删除失败");
        }
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData("删除成功");
        return response;
    }

    @GetMapping("/giftCountByUser")
    public ETResponse giftCountByUser(){
        List<GiftCountResp> gifts = giftService.giftCountByUser();

        ETResponse response = new ETResponse();
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData(gifts);
        return response;
    }
    /**
     * 用户往下
     * @return
     */

    @GetMapping("/getGiftByUser")
    public ETResponse getGiftByUser(@RequestParam(required = false,defaultValue = "1") int pageNum, @RequestParam(required = false,defaultValue = "2") int pageSize){
        Page<Gift> gifts = giftService.getGiftByUser(pageNum,pageSize);
        //List<Gift> gifts = giftService.getGiftByUser();

        ETResponse response = new ETResponse();
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData(gifts);
        return response;
    }

    @PostMapping("/addGiftOrder")
    public ETResponse addGiftOrder(@RequestBody GiftReq giftReq){
        ETResponse response = new ETResponse();
        int userId = Integer.parseInt(giftReq.getToken());
        Gift gift = giftService.getCountById(giftReq.getGiftId());
        //判断积分是否够
        int jifen = giftService.getJiFenById(userId);

        if(jifen<gift.getPrice()){
            response.setCode(ETEnums.ERROR.getCode());
            response.setMsg(ETEnums.ERROR.getMsg());
            response.setData("积分不足");
            return response;
        }
        //是否有库存
        if(gift.getCount1() == 0){
            response.setCode(ETEnums.ERROR.getCode());
            response.setMsg(ETEnums.ERROR.getMsg());
            response.setData("库存不足");
            return response;
        }

        int flag = giftService.addGiftOrder(userId,giftReq.getGiftId());


        if (flag<0){
            response.setCode(ETEnums.ERROR.getCode());
            response.setMsg(ETEnums.ERROR.getMsg());
            response.setData("兑换失败");
        }

        //去减库存
        int result = giftService.delGiftCount(giftReq.getGiftId());
        //减积分
        int result1 = giftService.delUserJiFen(userId,gift.getPrice());

        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData("兑换成功");
        return response;
    }


    @GetMapping("/queryGiftOrderByUser")
    public ETResponse queryGiftOrderByUser(@RequestParam(required = false,defaultValue = "1") int pageNum, @RequestParam(required = false,defaultValue = "2") int pageSize,@RequestParam("id") String id){
        int userId = Integer.parseInt(id);
        Page<GiftResp> gifts = giftService.queryGiftOrderByUser(userId,pageNum,pageSize);

        ETResponse response = new ETResponse();
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData(gifts);
        return response;
    }


}
